import uuid
from abc import abstractmethod

import pygame


class UISprite(pygame.sprite.Sprite):
    def __init__(self, surface, rect=None, as_rect=False):
        super().__init__()
        self.uuid = uuid.uuid4().hex
        self.checked = False
        self.image = surface if not as_rect else pygame.transform.scale(surface, rect.size)
        self.rect = rect if rect else self.image.get_rect()
        from constants import game
        game.list_mouse_button_down.append(self._Click)
        game.list_mouse_button_up.append(self._Release)

    def Destroy(self):
        from constants import game
        game.list_mouse_button_down.remove(self._Click)
        game.list_mouse_button_up.remove(self._Release)
        game.ui_group.remove(self)

    def _Click(self, pos, buttons):
        import locals
        if locals.mouse_move_focus_sprite == self:
            self.checked = True
            self.Click(pos, buttons)

    def _Release(self, pos, buttons):
        if self.checked:
            self.checked = False
            self.Release(pos, buttons)

    @abstractmethod
    def Click(self, pos, buttons):
        ...

    @abstractmethod
    def Release(self, pos, buttons):
        ...
